package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.CarBrand;
import com.tr.rentcar.model.bean.CarStyle;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class CarBrandDao implements ResultHandler<CarBrand> {
	private MySqlDb db=MySqlDb.getInstance();

	@Override
	public CarBrand doHander(Map<String, Object> row) {
		CarBrand cb=new CarBrand();
		cb.setCbId(Integer.parseInt(row.get("cb_id")+""));
		cb.setCbName(row.get("cb_name")+"");
		cb.setCbLogo(row.get("cb_logo")+"");
		return cb;
	}
	//获取所有品牌
	public List<CarBrand> getAll() throws SQLException{
		String sql="select * from car_brand";
		return db.executeQuery(sql, this);
	}
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean findbyiddelete(Integer id) throws SQLException{
		String sql="delete from car_brand where cb_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(id))>0;
	}
	/*
	 通过id找到该对象
	 */
	public CarBrand findbyid(Integer id) throws SQLException{
		String sql="select * from car_brand where cb_id=?";
		return db.queryUninResult(sql, new GlobalParamHandler(id),this);
	}
	
	/*
	 * 修改车品牌
	 */
	public boolean update( String cbName,String newFileName,Integer id) throws SQLException{
		String sql="update car_brand set cb_name=?,cb_logo=? where cb_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(cbName,newFileName,id))>0;
	}
	/*
	 * 修改车品牌(图片不变)
	 */
	public boolean updatei( String cbName,Integer id) throws SQLException{
		String sql="update car_brand set cb_name=? where cb_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(cbName,id))>0;
	}
	/**
	 * 添加记录(无图)
	 */
	public boolean insert(String name) throws SQLException{
		String sql="insert into car_brand(cb_name) values(?)";
		return db.executeUpdate(sql, new GlobalParamHandler(name))>0;
	}
	/**
	 * 添加记录(有图)
	 */
	public boolean insert2(String name,String logo) throws SQLException{
		String sql="insert into car_brand(cb_name,cb_logo) values(?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(name,logo))>0;
	}
	/*
	 通过名字找到该对象
	 */
	public CarBrand findbyname(String cbname) throws SQLException{
		String sql="select * from car_brand where cb_name=?";
		return db.queryUninResult(sql, new GlobalParamHandler(cbname),this);
	}

}
